﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using Abp.UI;
using CPPProject.CPP.Dto;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPProject.CPP
{
    /*菜单表*/
    public class CPPRoleMenuAppservice : AsyncCrudAppService<CPPRoleMenu, CPPRoleMenuDto, int, PagedResultRequestDto, CreateCPPRoleMenuDto, UpdateCPPRoleMenuDto>
    {
        private readonly IObjectMapper _objectMapper;
        public CPPRoleMenuAppservice(
            IRepository<CPPRoleMenu, int> repository,
            IObjectMapper objectMapper) : base(repository)
        {
            _objectMapper = objectMapper;
        }

        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public async Task<List<GetDoawRowDto>> GetMenuList(int parentId=0)
        {
            List<CPPRoleMenu> lists = new List<CPPRoleMenu>();
            lists = await Repository.GetAll().Where(o => o.ParentId == parentId && o.Hidden==false).ToListAsync();
            List<GetDoawRowDto> list = new List<GetDoawRowDto>();
            for (int i = 0; i < lists.Count; i++)
            {
                GetDoawRowDto getDoawRow = new GetDoawRowDto();
                getDoawRow.value = lists[i].Id.ToString();
                getDoawRow.label = lists[i].Name;
                list.Add(getDoawRow);
            }
            return list;
        }

        /// <summary>
        /// override重写添加方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<CPPRoleMenuDto> CreateAsync(CreateCPPRoleMenuDto input)
        {
            var lists = await Repository.GetAll().Where(o => o.Name == input.Name || o.Path == input.Path).ToListAsync();
            if (lists.Count <= 0)
            {
                return await base.CreateAsync(input);
            }
            else
            {
                throw new UserFriendlyException(input.Name + "或者" + input.Path + "已经存在");
            }
        }

        /// <summary>
        /// override重写修改方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<CPPRoleMenuDto> UpdateAsync(UpdateCPPRoleMenuDto input)
        {
            var lists = await Repository.GetAll().Where(o => o.Name == input.Name || o.Path == input.Path).ToListAsync();
            if (lists.Count <= 0)
            {
                return await base.UpdateAsync(input);
            }
            else
            {
                throw new UserFriendlyException(input.Name + "或者" + input.Path + "已经存在");
            }
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">用户ID</param>
        /// <returns></returns>
        public async Task BatchDelete(long[] ids)
        {
            if (ids != null && ids.Count() > 0)
            {
                await Repository.DeleteAsync(it => ids.Contains(it.Id));
            }
        }
    }
}
